|
|
|
|
|
|
|
Listing 14.3 LIMITED.ZIPProgramming the KeyPress Event to Screen User Input |
|
|
|
|
|
|
|
|
Private Sub txtCharSet_KeyPress(KeyAscii As Integer)
If KeyAscii = 8 Then Exit Sub
Select Case mCharAccept
Case 0 'Any character is acceptable
Exit Sub
Case 1 'Only numbers may be entered
If KeyAscii >= 48 And KeyAscii <= 57 Then
Exit Sub
Else
KeyAscii = 0
Beep
RaiseEvent UserError
End If
Case 2 'Only letters may be entered
If KeyAscii >= 65 And KeyAscii <= 90 Then
Exit Sub
ElseIf KeyAscii >= 97 And KeyAscii <= 122 Then
Exit Sub
Else
KeyAscii = 0
Beep
RaiseEvent UserError
End If
End Select
End Sub |
|
|
|
|
|
|
|
|
The code in Listing 14.3 is fairly simple. KeyAscii represents the typed character, which is checked for validity by Select Case and If statements. If the character falls outside an acceptable range, the control beeps and raises the UserError event. |
|
|
|
|
|
|
|
|
Testing the Limited Character Text Box |
|
|
|
|
|
|
|
|
After you enter all the code for the control, you can test the TxtCharLimit control by following these steps: |
|
|
|
|
|
|
|
|
2. Add a Standard EXE project to the project group. (You may have already taken this step.) |
|
|
|
|
|
|
|
|
3. Close the design and Code windows for the user control. Notice that an icon for your control appears in the Toolbox. |
|
|
|
|
|
|
|
|
4. Add an instance of the TxtCharLimit control to the form in the test application. |
|
|
|
|
|
|
|
|
5. Set the properties of the control. |
|
|
|
|
|
|
|
|
6. Run the test program, and try out the control. Try setting the CharAccept property to different values to verify that it accepts only the keystrokes you want. |
|
|
|
|
|